package site.yunnong.atvris.service.helper;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.dao.domain.User;
import site.yunnong.atvris.service.domain.UmsUserDetails;

/**
 * @author zjh
 * @date 2021/9/8 14:43
 */
public class OperatorHelper {
    
    public static User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UmsUserDetails userDetails = (UmsUserDetails) authentication.getPrincipal();
        if (!userDetails.isEnabled()) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "该账户已被封禁");
        }
        return userDetails.getUser();
    }
    
    public static Long getLoginUuid() {
        return getCurrentUser().getUuid();
    }
    
    public static boolean isLogin() {
        try {
            User user = getCurrentUser();
            if (user == null) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
